<?php

/**
 * This file is part of workerman.
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the MIT-LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @author walkor<walkor@workerman.net>
 * @copyright walkor<walkor@workerman.net>
 * @link http://www.workerman.net/
 * @license http://www.opensource.org/licenses/mit-license.php MIT License
 */

namespace Consumer;

use \Exception;

/**
 * 订单处理
 * @author 
 */
class Order extends Controller {

	//支付订单
	public function payOrder($data){
		try{
			$pay_data = [];
			$pay_data['member_id'] = $data[0];//用户ID
			$pay_data['pay_type'] = $data[1]; //支付方式
			$pay_data['order_sn'] = $data[2];//订单sn
			$pay_data['money'] = $data[3];//订单金额
			if(empty($pay_data['member_id'])){
				throw new Exception('用户ID不能为空');
			}
			if(empty($pay_data['pay_type'])){
				throw new Exception('支付方式不能为空');
			}
			if(empty($pay_data['order_sn'])){
				throw new Exception('订单编号不能为空');
			}
			if(empty($pay_data['money'])){
				throw new Exception('订单金额不能为空');
			}
			if($pay_data['pay_type']==WX_PAY){
				return $this->wxPay($pay_data);
			}elseif($pay_data['pay_type']==ALI_PAY){
				return $this->aliPay($pay_data);
			}
		}catch(Exception $e){
			$this->addLog('payOrder',$e->getMessage(),'error');
			return $this->returnJson($e->getMessage(), 300);
		}
	}
	
	
	//微信自动扣款
	private function wxPay($data){
		$result =[];
		$result['member_id'] = $data['member_id'];//用户ID
		$result['order_id'] = $data['order_sn'];//订单编号
		$result['money'] = $data['money'];//订单金额
		$result['pay_type'] = $data['pay_type'];//支付方式 [2-微信,1-支付宝]
		try{
			$url = http_build_query($result);
			$url = PAY_ORDER_URL.'?'.$url;
			//get请求
			$data = $this->curlGet($url);
			if(empty($data)){
				throw new Exception('处理订单url错误,url:'.$url);
			}
			$data = json_decode($data,true);
			$content = ',url:'.$url.',content:'.json_encode($data);
			if($data['code']!=200){
				throw new Exception($content);
			}
			//扣款成功
			$this->setUserDoorStatus($result['member_id'],DOOR_CLOSED_YES);
			//添加成功日志
			$this->addLog('wxPay',$content,'success');
			return true;
		}catch(Exception $e){
			//添加失败日志
			$this->addLog('wxPay',$e->getMessage(),'error');
			//扣款失败
			$this->setUserDoorStatus($result['member_id'],DOOR_CLOSED_YES_FAIL);
			throw new Exception($e->getMessage());
		}
	}
	
	//支付宝自动扣款
	private function aliPay($data){
		$result =[];
		$result['member_id'] = $data['member_id'];//用户ID
		$result['order_id'] = $data['order_sn'];//订单编号
		$result['money'] = $data['money'];//订单金额
		$result['pay_type'] = $data['pay_type'];//支付方式 [2-微信,1-支付宝]
		try{
			$url = http_build_query($result);
			$url = PAY_ORDER_URL.'?'.$url;
			//get请求
			$data = $this->curlGet($url);
			if(empty($data)){
				throw new Exception('处理订单url错误,url:'.$url);
			}
			$data = json_decode($data,true);
			$content = ',url:'.$url.',content:'.json_encode($data);
			if($data['code']!=200){
				throw new Exception($content);
			}
			//扣款成功
			$this->setUserDoorStatus($result['member_id'],DOOR_CLOSED_YES);
			//添加成功日志
			$this->addLog('alPay',$content,'success');
			return true;
		}catch(Exception $e){
			//添加失败日志
			$this->addLog('alPay',$e->getMessage(),'error');
			//扣款失败
			$this->setUserDoorStatus($result['member_id'],DOOR_CLOSED_YES_FAIL);
			throw new Exception($e->getMessage());
		}
	}
	
	//根据用户获取门ID
	private function getDoorIdByUserId($user_id){
		$redis = $this->getRedisModule();
		$key = USER_DOOR_STATUS.$user_id;
		return $redis->get($key);
	}
	
	public function test($data){
		$this->setUserDoorStatus($data['user_id'],$data['status']);
	}
	
	//设置用户和门的状态
	private function setUserDoorStatus($user_id,$status){
		$door_id = $this->getDoorIdByUserId($user_id);
		$redis = $this->getRedisModule();
		$door_key = DOOR_STATUS.$door_id;
		$redis->multi();
		$redis->set($door_key,$status);
		//用户状态
		$key = USER_STATUS.$user_id;
		$redis->set($key,$status);
		//线上用户列表
		$redis->rpush(ON_LINE_USER_ID_LIST,$user_id);
		$redis->exec();
		return true;
	}
	
	//获取redis module
    private function getRedisModule() {
        return Tool::getRedisModule();
    }

}
